//
//  HSongTableRowView.m
//  HMusicPlayer
//
//  Created by Hament on 14/9/19.
//  Copyright (c) 2014年 HJ. All rights reserved.
//

#import "HSongTableRowView.h"
#import "HCoreConfig.h"
#import "HskinManager.h"



@implementation HSongTableRowView



-(void)awakeFromNib{


    self.customBGColor=[HCoreConfig shareConfig].currentSkinManager.rightListCellBGColor1;

}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}




//选择后的row的背景颜色
- (void)drawSelectionInRect:(NSRect)dirtyRect {
    // Check the selectionHighlightStyle, in case it was set to None
    if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
        
        CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
        [[HCoreConfig shareConfig].currentSkinManager.rightListCellHighlightBGColor setFill];
        CGContextFillRect(context, NSRectToCGRect(dirtyRect));

    }
}



- (void)drawBackgroundInRect:(NSRect)dirtyRect {
    // Custom background drawing. We don't call super at all.
    //[self.backgroundColor set];
    if (self.mouseInside) {
        //绘制颜色
        CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
        [[HCoreConfig shareConfig].currentSkinManager.rightListCellHighlightBGColor setFill];
        CGContextFillRect(context, NSRectToCGRect(dirtyRect));
    } else {
        CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
        [self.customBGColor setFill];
        
        CGContextFillRect(context, NSRectToCGRect(dirtyRect));
        
    }
    
}

- (void)setFrame:(NSRect)frameRect {
    [super setFrame:frameRect];
    // We need to invalidate more things when live-resizing since we fill with a gradient and stroke
    if ([self inLiveResize]) {
        // Redraw everything if we are using a gradient
        if (self.selected || self.mouseInside) {
            [self setNeedsDisplay:YES];
        } else {
            // Redraw our horizontal grid line, which is a gradient
            //[self setNeedsDisplayInRect:[self separatorRect]];
        }
    }
}



#pragma mark actionevent


- (void)setMouseInside:(BOOL)value {
    if (_mouseInside != value) {
        _mouseInside = value;
        [self setNeedsDisplay:YES];
    }
}

- (BOOL)mouseInside {
    return _mouseInside;
}

- (void)ensureTrackingArea {
    if (_trackingArea == nil) {
        _trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:NSTrackingInVisibleRect | NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    }
}




- (void)createTrackingArea {
    
    //慎用trackoptions
    _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:NSTrackingInVisibleRect | NSTrackingActiveInActiveApp | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
    
    [self addTrackingArea:_trackingArea];
    
    //考虑控件还未被监听时鼠标就已经在里面的情况
    NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream];
    mouseLocation = [self convertPoint: mouseLocation
                              fromView: nil];
    
    if (NSPointInRect(mouseLocation, [self bounds])){
        [self mouseEntered: nil];
    }
    else
    {
        [self mouseExited: nil];
    }
    
}



- (void)updateTrackingAreas {
    [self removeTrackingArea:_trackingArea];
    [self createTrackingArea];
    [super updateTrackingAreas];
}



- (void)mouseEntered:(NSEvent *)theEvent {
    self.mouseInside = YES;
}

- (void)mouseExited:(NSEvent *)theEvent {
    self.mouseInside = NO;
}


@end
